package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.AccountMapper;
import org.csu.mypetstore.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @GetMapping("/all")
    public String viewAllAccount(Model model) {
        List<Account> accountList = accountService.getAccountList();
        model.addAttribute("accountList",accountList);
        return "account/allAccount";
    }

    @RequestMapping("/view")
    public String viewAccount(String userid, Model model) {
        Account account = accountService.getAccountByUserId(userid);
        model.addAttribute("account",account);
        return "account/viewAccount";
    }

    @GetMapping("/edit")
    public String editAccount(String userid,Model model) {
        Account account = accountService.getAccountByUserId(userid);
        model.addAttribute("account",account);
        return "account/editAccount";
    }

    @PostMapping("/editConfirm")
    public String editAccountConfirm(Account account, Model model, HttpServletResponse response) {
        accountService.updateAccount(account);
        model.addAttribute("account",account);
        return "redirect:/catalog/view";

    }
}
